﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Collections.ObjectModel
Imports System.Windows.Threading
Imports Microsoft.Samples.WindowsPhoneCloud.StorageClient
Imports System.Globalization
Imports System.Text

Namespace ViewModel
    Public Class QueueDetailsPageViewModel
        Inherits BaseViewModel
        Private ReadOnly dispatcher As Dispatcher
        Private _queue As ICloudQueue
        Private _isBusy As Boolean
        Private _queueMessageContent As String
        Private _message As String

        Public Sub New()
            Me.New(Deployment.Current.Dispatcher)
        End Sub

        Public Sub New(ByVal dispatcher As Dispatcher)
            Me.dispatcher = dispatcher
            Me.CloudQueueMessages = New ObservableCollection(Of CloudQueueMessage)()
        End Sub

        Public Property Queue() As ICloudQueue
            Get
                Return Me._queue
            End Get

            Set(ByVal value As ICloudQueue)
                Me._queue = value
                Me.NotifyPropertyChanged("Queue")
            End Set
        End Property

        Public Property IsBusy() As Boolean
            Get
                Return Me._isBusy
            End Get

            Set(ByVal value As Boolean)
                If Me._isBusy <> value Then
                    Me._isBusy = value
                    Me.NotifyPropertyChanged("IsBusy")
                End If
            End Set
        End Property

        Public Property QueueMessageContent() As String
            Get
                Return Me._queueMessageContent
            End Get

            Set(ByVal value As String)
                If Me._queueMessageContent <> value Then
                    Me._queueMessageContent = value
                    Me.NotifyPropertyChanged("QueueMessageContent")
                End If
            End Set
        End Property

        Public Property Message() As String
            Get
                Return Me._message
            End Get

            Set(ByVal value As String)
                If Me._message <> value Then
                    Me._message = value
                    Me.NotifyPropertyChanged("Message")
                End If
            End Set
        End Property

        Public Property CloudQueueMessages() As ObservableCollection(Of CloudQueueMessage)

        Public Sub QueueMessage()
            Me.Message = "Queing message..."
            Me.IsBusy = True
            Try
                Me.Queue.AddMessage(
                    New CloudQueueMessage With {.AsBytes = Encoding.UTF8.GetBytes(Me.QueueMessageContent)},
                    Sub(r)
                        Me.dispatcher.BeginInvoke(
                            Sub()
                                Me.Message = If(r.Exception Is Nothing, "Message successfully queued!", String.Format(CultureInfo.InvariantCulture, "Error: {0}", r.Exception.Message))
                                Me.IsBusy = False
                            End Sub)
                    End Sub)
            Catch ex As Exception
                Dim errorMessage = StorageClientExceptionParser.ParseDataServiceException(ex).Message
                Me.IsBusy = False
                Me.Message = String.Format(CultureInfo.InvariantCulture, "Error: {0}", errorMessage)
            End Try
        End Sub

        Public Sub DequeueMessage()
            Me.Message = "Dequeing message..."
            Me.IsBusy = True
            Try
                Me.Queue.GetMessage(
                    Sub(s)
                        If s.Exception Is Nothing Then
                            If s.Response Is Nothing Then
                                Me.dispatcher.BeginInvoke(
                                    Sub()
                                        Me.Message = "Queue is empty"
                                        Me.IsBusy = False
                                    End Sub)
                            Else
                                Me.Queue.DeleteMessage(
                                    s.Response,
                                    Sub(r)
                                        Me.dispatcher.BeginInvoke(
                                        Sub()
                                            If r.Exception Is Nothing Then
                                                Me.CloudQueueMessages.Add(s.Response)
                                                Me.Message = "Message successfully dequeued!"
                                            Else
                                                Me.Message = String.Format(CultureInfo.InvariantCulture, "Error: {0}", r.Exception.Message)
                                            End If

                                            Me.IsBusy = False
                                        End Sub)
                                    End Sub)
                            End If
                        Else
                            Me.dispatcher.BeginInvoke(
                                Sub()
                                    Me.Message = String.Format(CultureInfo.InvariantCulture, "Error: {0}", s.Exception.Message)
                                    Me.IsBusy = False
                                End Sub)
                        End If
                    End Sub)
            Catch ex As Exception
                Dim errorMessage = StorageClientExceptionParser.ParseDataServiceException(ex).Message
                Me.IsBusy = False
                Me.Message = String.Format(CultureInfo.InvariantCulture, "Error: {0}", errorMessage)
            End Try
        End Sub
    End Class
End Namespace
